package com.ruoyi.wallet.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.distributor.domain.DistributionUser;
import com.ruoyi.wallet.mapper.DistributionWalletMapper;
import com.ruoyi.wallet.domain.DistributionWallet;
import com.ruoyi.wallet.service.IDistributionWalletService;

/**
 * 分销钱包Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-11
 */
@Service
public class DistributionWalletServiceImpl implements IDistributionWalletService 
{
    @Autowired
    private DistributionWalletMapper distributionWalletMapper;

    /**
     * 查询分销钱包
     *
     * @param distributorId 分销商id
     * @return 分销钱包
     */
    @Override
    public DistributionWallet selectWalletByDistributorId(Long distributorId) {
        return distributionWalletMapper.selectWalletByDistributorId(distributorId);
    }

    /**
     * 查询分销钱包
     * 
     * @param walletId 分销钱包主键
     * @return 分销钱包
     */
    @Override
    public DistributionWallet selectDistributionWalletByWalletId(Long walletId)
    {
        return distributionWalletMapper.selectDistributionWalletByWalletId(walletId);
    }

    /**
     * 查询分销钱包列表
     * 
     * @param distributionWallet 分销钱包
     * @return 分销钱包
     */
    @Override
    public List<DistributionWallet> selectDistributionWalletList(DistributionWallet distributionWallet)
    {
        return distributionWalletMapper.selectDistributionWalletList(distributionWallet);
    }

    /**
     * 新增分销钱包
     * 
     * @param distributionWallet 分销钱包
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDistributionWallet(DistributionWallet distributionWallet)
    {
        distributionWallet.setCreateTime(DateUtils.getNowDate());
        return distributionWalletMapper.insertDistributionWallet(distributionWallet);
    }

    /**
     * 修改分销钱包
     * 
     * @param distributionWallet 分销钱包
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDistributionWallet(DistributionWallet distributionWallet)
    {
        distributionWallet.setUpdateTime(DateUtils.getNowDate());
        return distributionWalletMapper.updateDistributionWallet(distributionWallet);

    }

    /**
     * 批量删除分销钱包
     * 
     * @param walletIds 需要删除的分销钱包主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDistributionWalletByWalletIds(Long[] walletIds)
    {
        distributionWalletMapper.deleteDistributionUserByDistributorIds(walletIds);
        return distributionWalletMapper.deleteDistributionWalletByWalletIds(walletIds);
    }

    /**
     * 删除分销钱包信息
     * 
     * @param walletId 分销钱包主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDistributionWalletByWalletId(Long walletId)
    {
        distributionWalletMapper.deleteDistributionUserByDistributorId(walletId);
        return distributionWalletMapper.deleteDistributionWalletByWalletId(walletId);
    }

    /**
     * 计算今日佣金
     *
     * @param distributorId 分销商id
     * @return 佣金
     */
    @Override
    public BigDecimal calculateTodayCommission(Long distributorId) {
        // 1. 从数据库查询今日产生的佣金
//        Map<String, Object> params = new HashMap<>();
//        params.put("distributorId", distributorId);
//        params.put("startDate", DateUtils.getTodayStart());
//        params.put("endDate", DateUtils.getTodayEnd());
//
//        BigDecimal todayCommission = distributionWalletMapper.selectCommissionSum(params);
//
//        // 2. 如果查询结果为null，返回0
//        return todayCommission != null ? todayCommission : BigDecimal.ZERO;
        return BigDecimal.ZERO;
    }

    /**
     * 计算本月佣金
     *
     * @param distributorId 分销商id
     * @return 佣金
     */
    @Override
    public BigDecimal calculateMonthlyCommission(Long distributorId) {
        // 1. 从数据库查询本月产生的佣金
//        Map<String, Object> params = new HashMap<>();
//        params.put("distributorId", distributorId);
//        params.put("startDate", DateUtils.getMonthStart());
//        params.put("endDate", DateUtils.getMonthEnd());
//
//        BigDecimal monthlyCommission = distributionWalletMapper.selectCommissionSum(params);
//
//        // 2. 如果查询结果为null，返回0
//        return monthlyCommission != null ? monthlyCommission : BigDecimal.ZERO;
        return BigDecimal.ZERO;
    }

}
